package com.zaker.touchtv.modules;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.GravityCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.zaker.touchtv.R;


public class TouchTvMainLayout extends FrameLayout {

  private final static String LOG_TAG = "TouchTvMainLayout";

  private TextView mVersionNameView;

  private ImageView mTouchLogoView;

  private TextView mPublishView;

  private TextView mSubscribeView;

  private TextView mTestView;

  private TextView mSelectedTipView;

  private TextView mSelectedDeviceView;

  private ImageView mSelectedIconView;

  private TextView mLoginView;

  private int mBtnWidth;
  private int mBtnBottomMargin;
  private int mSelectedBtnTopOrBottomMargin;
  private Paint mSeparatorLinePaint;
  private PaintFlagsDrawFilter mAdjustLineDrawFilter;
  private int mLineStrokeWidth;
  private int mLineY = -1;
  private int mLogoMinTopOrBottomMargin;

  public static TouchTvMainLayout newInstance(@NonNull LayoutInflater layoutInflater,
                                              @Nullable ViewGroup rootViewGroup,
                                              boolean attachToRoot) {
    View resultView = layoutInflater.inflate(R.layout.touch_tv_layout, rootViewGroup, attachToRoot);
    if (attachToRoot) {
      return (TouchTvMainLayout) resultView.findViewById(R.id.touch_tv_content);
    } else {
      return (TouchTvMainLayout) resultView;
    }
  }

  public TouchTvMainLayout(Context context, AttributeSet attrs) {
    super(context, attrs);

    init();
  }

  private void init() {

    setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
        LayoutParams.MATCH_PARENT));
    mBtnWidth = getResources().getDimensionPixelSize(R.dimen.main_btn_width);
    mBtnBottomMargin = getResources().getDimensionPixelSize(R.dimen.main_btn_bottom_margin);
    mSelectedBtnTopOrBottomMargin = getResources()
        .getDimensionPixelSize(R.dimen.main_selector_btn_top_bottom_margin);
    mSeparatorLinePaint = new Paint();
    mSeparatorLinePaint.setAntiAlias(false);
    int lineColor = ResourcesCompat.getColor(getResources(), R.color.colorDivider, null);
    mAdjustLineDrawFilter = new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG);
    mLineStrokeWidth = getResources().getDimensionPixelSize(R.dimen.divider_height);
    mSeparatorLinePaint.setColor(lineColor);
    mSeparatorLinePaint.setStrokeWidth(mLineStrokeWidth);
    mLogoMinTopOrBottomMargin = getResources()
        .getDimensionPixelSize(R.dimen.main_logo_min_top_bottom_margin);
  }

  @Override
  protected void onFinishInflate() {

    mTouchLogoView = (ImageView) getChildAt(0);
    mPublishView = (TextView) getChildAt(1);
    mSubscribeView = (TextView) getChildAt(2);
    mTestView = (TextView) getChildAt(3);
    mSelectedTipView = (TextView) getChildAt(4);
    mSelectedDeviceView = (TextView) getChildAt(5);
    mSelectedIconView = (ImageView) getChildAt(6);
    mLoginView = (TextView) getChildAt(7);
    mVersionNameView = (TextView) getChildAt(8);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    int widthHint = MeasureSpec.getSize(widthMeasureSpec);
    int heightHint = MeasureSpec.getSize(heightMeasureSpec);

    if (getParent() instanceof ViewGroup) {
      heightHint = Math.max(((ViewGroup) getParent()).getMeasuredHeight(), heightHint);
    }

    heightHint -= getTop() + getPaddingTop();

    Log.i(LOG_TAG, " widthHint: " + widthHint + " heightHint: " + heightHint + " getPaddingTop: "
        + getPaddingTop() + " getPaddingBottom: " + getPaddingBottom()
        + " getTop: " + getTop());

    measureChildWithMargins(mLoginView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mSelectedDeviceView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mSelectedIconView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mSelectedTipView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mTestView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mSubscribeView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mPublishView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mTouchLogoView, widthMeasureSpec, 0, heightMeasureSpec, 0);
    measureChildWithMargins(mVersionNameView, widthMeasureSpec, 0, heightMeasureSpec, 0);

    final int loginHeight = mLoginView.getMeasuredHeight();
    final int selectedDeviceHeight = mSelectedDeviceView.getMeasuredHeight();
    final int selectedTipHeight = mSelectedTipView.getMeasuredHeight();
    final int testHeight = mTestView.getMeasuredHeight();
    final int subscribeHeight = mSubscribeView.getMeasuredHeight();
    final int publishHeight = mPublishView.getMeasuredHeight();

    final int desiredMinHeight = loginHeight + selectedDeviceHeight
        + selectedTipHeight + testHeight + subscribeHeight
        + publishHeight + loginHeight + mLineStrokeWidth
        + mLogoMinTopOrBottomMargin * 2 + mSelectedBtnTopOrBottomMargin * 2
        + mBtnBottomMargin * 2;

    heightHint = Math.max(desiredMinHeight, heightHint);

    measureChildForJustSize(widthHint, heightHint);

    LayoutParams versionLayoutParams = (LayoutParams) mVersionNameView.getLayoutParams();
    versionLayoutParams.gravity = Gravity.TOP | GravityCompat.START;

    setMeasuredDimension(resolveSize(widthHint, widthMeasureSpec),
        resolveSize(heightHint, heightMeasureSpec));
  }

  private void measureChildForJustSize(final int widthHint, final int heightHint) {

    LayoutParams layoutParams = (LayoutParams) mLoginView.getLayoutParams();
    layoutParams.topMargin = heightHint - mLoginView.getMeasuredHeight();
    layoutParams.leftMargin = (widthHint - mLoginView.getMeasuredWidth()) / 2;

    mLineY = layoutParams.topMargin - mLineStrokeWidth;

    layoutParams = (LayoutParams) mSelectedDeviceView.getLayoutParams();
    layoutParams.topMargin = mLineY - mSelectedDeviceView.getMeasuredHeight()
        - mSelectedBtnTopOrBottomMargin;
    layoutParams.leftMargin = (widthHint - mSelectedDeviceView.getMeasuredWidth()) / 2;

    final int deviceTopMargin = layoutParams.topMargin;
    final int deviceLeftMargin = layoutParams.leftMargin;
    layoutParams = (LayoutParams) mSelectedIconView.getLayoutParams();
    layoutParams.topMargin = deviceTopMargin
        + (mSelectedDeviceView.getMeasuredHeight() - mSelectedIconView.getMeasuredHeight()) / 2;
    layoutParams.leftMargin = deviceLeftMargin + mSelectedDeviceView.getMeasuredWidth()
        - mSelectedIconView.getMeasuredWidth();

    final LayoutParams tipLayoutParams = (LayoutParams) mSelectedTipView.getLayoutParams();
    tipLayoutParams.leftMargin = (widthHint - mSelectedDeviceView.getMeasuredWidth()) / 2;
    tipLayoutParams.topMargin = deviceTopMargin - mSelectedTipView.getMeasuredHeight();

    final LayoutParams testLayoutParams = (LayoutParams) mTestView.getLayoutParams();
    testLayoutParams.leftMargin = (widthHint - mTestView.getMeasuredWidth()) / 2;
    testLayoutParams.topMargin = tipLayoutParams.topMargin
        - mTestView.getMeasuredHeight() - mSelectedBtnTopOrBottomMargin;

    final LayoutParams subscribeLayoutParams = (LayoutParams) mSubscribeView.getLayoutParams();
    subscribeLayoutParams.leftMargin = (widthHint - mSubscribeView.getMeasuredWidth()) / 2;
    subscribeLayoutParams.topMargin = testLayoutParams.topMargin
        - mSubscribeView.getMeasuredHeight() - mBtnBottomMargin;

    final LayoutParams publishLayoutParams = (LayoutParams) mPublishView.getLayoutParams();
    publishLayoutParams.leftMargin = (widthHint - mPublishView.getMeasuredWidth()) / 2;
    publishLayoutParams.topMargin = subscribeLayoutParams.topMargin
        - mPublishView.getMeasuredHeight() - mBtnBottomMargin;
    publishLayoutParams.height = mPublishView.getMeasuredHeight();
    publishLayoutParams.width = mPublishView.getMeasuredWidth();

    final LayoutParams logoLayoutParams = (LayoutParams) mTouchLogoView.getLayoutParams();
    logoLayoutParams.leftMargin = (widthHint - mTouchLogoView.getMeasuredWidth()) / 2;
    logoLayoutParams.topMargin = (publishLayoutParams.topMargin
        - mTouchLogoView.getMeasuredHeight()) / 2;
  }

  @Override
  protected void dispatchDraw(Canvas canvas) {
    super.dispatchDraw(canvas);
    if (mLineY != -1) {
      canvas.setDrawFilter(mAdjustLineDrawFilter);
      final float startX = (getWidth() - mBtnWidth) / 2f;
      canvas.drawLine(startX, mLineY, startX + mBtnWidth, mLineY, mSeparatorLinePaint);
    }
  }

  public TextView getLoginView() {
    return mLoginView;
  }

  public TextView getSelectedDeviceView() {
    return mSelectedDeviceView;
  }

  public void setVersionName(String versionName) {
    if (mVersionNameView == null) {
      return;
    }
    mVersionNameView.setText(versionName);
  }
}




















